/*******************************************************************************
 *   Filename:       bsp_sht3x_dis.h
 *   Revised:        All copyrights reserved to Roger.
 *   Date:           2016-09-24
 *   Revision:       v1.0
 *   Writer:	     Roger-WY.
 *
 *   Description:    数字式温湿度传感器模块驱动  头文件
 *******************************************************************************/
#ifndef __BSP_SHT3X_DIS_H__
#define __BSP_SHT3X_DIS_H__


#include "stm32g0xx_hal.h"


#define SHT3XDIS_USE_HARD_I2C
//#define EN_SHT3X_ALERT      //系统是否使能报警引脚
//#define EN_SHT3X_NRESET     //系统是否使能硬件复位引脚


#define SHT3X_I2C_ADDR  (0x44)      //ADDR引脚接在VSS上
//#define SHT3X_I2C_ADDR  (0x45)    //ADDR引脚接在VDD上



/* 宏定义单片机数据存储模式（大端or小端）*/
//Processor endian system
//#define BIG ENDIAN   //e.g. Motorola (not tested at this time)
#define LITTLE_ENDIAN  //e.g. PIC, 8051, NEC V850 STM32


// Sensor Commands    传感器命令
typedef enum{
    CMD_READ_SERIALNBR  = 0x3780, // read serial number       读取串口数字SN号
    CMD_READ_STATUS     = 0xF32D, // read status register     读取状态寄存器
	CMD_CLEAR_STATUS    = 0x3041, // clear status register    清楚状态寄存器
	CMD_HEATER_ENABLE   = 0x306D, // enabled heater           使能加热
	CMD_HEATER_DISABLE  = 0x3066, // disable heater           失能加热
    CMD_SOFT_RESET      = 0x30A2, // soft reset               软件复位
	CMD_MEAS_CLOCKSTR_H = 0x2C06, // meas. clock stretching, high rep.
	CMD_MEAS_CLOCKSTR_M = 0x2C0D, // meas. clock stretching, medium rep.
	CMD_MEAS_CLOCKSTR_L = 0x2C10, // meas. clock stretching, low rep.
	CMD_MEAS_POLLING_H  = 0x2400, // meas. no clock stretching, high rep.
	CMD_MEAS_POLLING_M  = 0x240B, // meas. no clock stretching, medium rep.
	CMD_MEAS_POLLING_L  = 0x2416, // meas. no clock stretching, low rep.
	CMD_MEAS_PERI_05_H  = 0x2032, // meas. periodic 0.5 mps, high rep.
	CMD_MEAS_PERI_05_M  = 0x2024, // meas. periodic 0.5 mps, medium rep.
	CMD_MEAS_PERI_05_L  = 0x202F, // meas. periodic 0.5 mps, low rep.
	CMD_MEAS_PERI_1_H   = 0x2130, // meas. periodic 1 mps, high rep.
	CMD_MEAS_PERI_1_M   = 0x2126, // meas. periodic 1 mps, medium rep.
	CMD_MEAS_PERI_1_L   = 0x212D, // meas. periodic 1 mps, low rep.
	CMD_MEAS_PERI_2_H   = 0x2236, // meas. periodic 2 mps, high rep.
	CMD_MEAS_PERI_2_M   = 0x2220, // meas. periodic 2 mps, medium rep.
	CMD_MEAS_PERI_2_L   = 0x222B, // meas. periodic 2 mps, low rep.
	CMD_MEAS_PERI_4_H   = 0x2334, // meas. periodic 4 mps, high rep.
	CMD_MEAS_PERI_4_M   = 0x2322, // meas. periodic 4 mps, medium rep.
	CMD_MEAS_PERI_4_L   = 0x2329, // meas. periodic 4 mps, low rep.
	CMD_MEAS_PERI_10_H  = 0x2737, // meas. periodic 10 mps, high rep.
	CMD_MEAS_PERI_10_M  = 0x2721, // meas. periodic 10 mps, medium rep.
	CMD_MEAS_PERI_10_L  = 0x272A, // meas. periodic 10 mps, low rep.
	CMD_FETCH_DATA      = 0xE000, // readout measurements for periodic mode
	CMD_R_AL_LIM_LS     = 0xE102, // read alert limits, low set
	CMD_R_AL_LIM_LC     = 0xE109, // read alert limits, low clear
	CMD_R_AL_LIM_HS     = 0xE11F, // read alert limits, high set
	CMD_R_AL_LIM_HC     = 0xE114, // read alert limits, high clear
	CMD_W_AL_LIM_LS     = 0x6100, // write alert limits, low set
	CMD_W_AL_LIM_LC     = 0x610B, // write alert limits, low clear
	CMD_W_AL_LIM_HS     = 0x611D, // write alert limits, high set
	CMD_W_AL_LIM_HC     = 0x6116, // write alert limits, high clear
    CMD_NO_SLEEP        = 0x303E,
}etSht3xCommands;

/* 枚举错误代码 */
typedef enum{
  NO_ERROR       = 0x00, // no error
  ACK_ERROR      = 0x01, // no acknowledgment error
  CHECKSUM_ERROR = 0x02, // checksum mismatch error
  TIMEOUT_ERROR  = 0x04, // timeout error
  PARM_ERROR     = 0x80, // parameter out of range error
}etSht3xError;

/* 枚举重复模式 */
typedef enum{
	REPEATAB_HIGH,   // high repeatability     高重复性
	REPEATAB_MEDIUM, // medium repeatability   中重复性
	REPEATAB_LOW,    // low repeatability      低重复性
}etSht3xRepeatab;

/* 枚举模式 */
typedef enum{
	MODE_CLKSTRETCH, // clock stretching
	MODE_POLLING,    // polling
}etSht3xMode;

/* 枚举读取频率 */
typedef enum{
	FREQUENCY_HZ5,  //  0.5 measurements per seconds
	FREQUENCY_1HZ,  //  1.0 measurements per seconds
	FREQUENCY_2HZ,  //  2.0 measurements per seconds
	FREQUENCY_4HZ,  //  4.0 measurements per seconds
	FREQUENCY_10HZ, // 10.0 measurements per seconds
}etSht3xFrequency;

//-- Typedefs ------------------------------------------------------------------
// Status-Register 状态寄存器
typedef union {
	uint16_t u16RegStatus;
	struct{
		#ifdef LITTLE_ENDIAN  // bit-order is little endian 小端模式
		uint16_t CrcStatus     : 1; // write data checksum status
		uint16_t CmdStatus     : 1; // command status
		uint16_t Reserve0      : 2; // reserved
		uint16_t ResetDetected : 1; // system reset detected
		uint16_t Reserve1      : 5; // reserved
		uint16_t T_Alert       : 1; // temperature tracking alert
		uint16_t RH_Alert      : 1; // humidity tracking alert
		uint16_t Reserve2      : 1; // reserved
		uint16_t HeaterStatus  : 1; // heater status
		uint16_t Reserve3      : 1; // reserved
		uint16_t AlertPending  : 1; // alert pending status
		#else                 // bit-order is big endian    大端模式
		uint16_t AlertPending  : 1;
		uint16_t Reserve3      : 1;
		uint16_t HeaterStatus  : 1;
		uint16_t Reserve2      : 1;
		uint16_t RH_Alert      : 1;
		uint16_t T_Alert       : 1;
		uint16_t Reserve1      : 5;
		uint16_t ResetDetected : 1;
		uint16_t Reserve0      : 2;
		uint16_t CmdStatus     : 1;
		uint16_t CrcStatus     : 1;
		#endif
	}bit;
} utSht3xRegStatus;

typedef struct
{
    /***********************************************
    * 描述： SHTxx测量数据结果放大10倍之后的值（整型）
    */
    int iTemp;
    int iHumi;
//    int DewPoint;
    /***********************************************
    * 描述： SHTxx测量数据结果（浮点型）
    */
    float fTemp;
    float fHumi;
    //float fDewpoint;
}stSht3xValue;



void bsp_Sht3xInit(uint8_t sht3xI2cAddr);
uint8_t bsp_Sht3xSoftReset(uint8_t sht3xI2cAddr);
uint8_t bsp_SHT3xReadSerialNumber(uint8_t sht3xI2cAddr,uint32_t *serialNbr);
uint8_t bsp_Sht3xStartPeriodicMeasurment(uint8_t sht3xI2cAddr,etSht3xRepeatab  repeatab,etSht3xFrequency freq);
uint8_t bsp_Sht3xReadFetchData(uint8_t sht3xI2cAddr,stSht3xValue *psht3xvalue);
uint8_t bsp_Sht3xReadStatus(uint8_t sht3xI2cAddr,utSht3xRegStatus *status);
uint8_t bsp_Sht3xClearAllAlertFlags(uint8_t sht3xI2cAddr);
uint8_t bsp_Sht3xEnableHeater(uint8_t sht3xI2cAddr);
uint8_t Bsp_Sht3xDisbaleHeater(uint8_t sht3xI2cAddr);

#ifdef EN_SHT3X_NRESET     //系统是否使能硬件复位引脚
void SHT3X_HardReset(void);
#endif
#ifdef EN_SHT3X_ALERT   /* SHT3X 传感器的报警引脚 */
uint8_t SHT3X_ReadAlert(void);
#endif


#endif
/***************************** (END OF FILE) **********************************/
